<?php

class BidAuction extends CFormModel
{

    public $auctionId;
    public $customerId;

    protected $_retry = false;

    protected $_auctionLimitAr;
    protected $_auction;


    public function init()
    {
        $this->_auctionLimitAr = new AuctionLimit();
    }

    public function rules()
    {
        return array(
            array('auctionId,customerId', 'required'),
            array('auctionId', 'hasAuction'),
            //array('auctionId', 'auctionLimit', 'winner_id' => $this->customerId ),
            //array('auctionId', 'auctionLock')
        );
    }

    public function hasAuction($attribute, $params)
    {
        $auctionPool = new AuctionPool();
        $auctionInfo = $auctionPool->findByAttributes(array(
            'auction_id' => $this->{$attribute},
            'status' => 1,
        ));
        if (!empty($auctionInfo)) {
            $this->_auction = $auctionInfo;
        } else {
            $this->addError($attribute, 'ext');
        }

    }

    public function auctionLimit($attribute = 'auctionId', $params = null)
    {
        $this->_auctionLimitAr->winner_id = $this->customerId;
        $limitList = $this->_auctionLimitAr->limitData();
        if ($limitList->limitNumber >= 5) {
            $this->addError($attribute, 'limit Count');
            return false;
        }
        $auctionList = explode(',', $limitList->limitList);
        if (in_array($this->auctionId, $auctionList)) {
            $this->addError($attribute, 'limit winner');
            return false;
        }
        return true;
    }

    public function auctionLock($attribute = 'auctionId', $params = null)
    {
        $expire = $this->_auction->end_time + 1;
        $lockName = 'bidAuctionLock:' . $this->auctionId;
        $lock = Yii::app()->dbRedis->setnx($lockName, $expire);
        Yii::app()->dbRedis->expire($lockName, $expire);
        if (empty($lock)) {
            return false;
        }
        return true;
    }

    public function unAuctionLock()
    {
        $lockName = 'bidAuctionLock:' . $this->auctionId;
        return Yii::app()->dbRedis->delete($lockName);
    }

    protected function bidProcess(){

    }

    public function bid()
    {
        $valid = $this->validate();
        if (!$valid) {
            return !$this->hasErrors();
        }

        //valid
        $expire = time() + 1;
        $transaction = Yii::app()->db->getCurrentTransaction();
        if (empty($transaction)) {
           
        }
        
        $transaction = Yii::app()->db->beginTransaction();
        
        try {

            do {
                $this->_retry = time()  < $expire;
                $valid = $this->auctionLimit();
                if (!$valid) {
                    return !$this->hasErrors();
                }
                $valid = $this->auctionLock();
                if (!$valid) {
                    continue;
                }

                $this->unAuctionLock();
                return true;
                usleep(100000);
            } while ($this->_retry);
            $this->addError('system', 'time_out');
        } catch (Exception $e) {
            $transaction->rollback();
            $this->addError('system', $e->getMessage());
        }


    }
}